package com.yuanfeng.job.job;

import com.yuanfeng.commoms.dto.order.AddInventoryDTO;
import com.yuanfeng.commoms.dto.order.CancelParaEntityDTO;
import com.yuanfeng.commoms.dto.order.RefundParamDTO;
import com.yuanfeng.commoms.dto.order.WapGoodsSaleSumDTO;
import com.yuanfeng.commoms.util.TimeStampUtil;
import com.yuanfeng.commoms.vo.goods.WapGoodsSaleSumUpdateVO;
import com.yuanfeng.job.dto.BusinessOvertimeOrderDTO;
import com.yuanfeng.job.dto.RefundParamTwoDTO;
import com.yuanfeng.job.feign.PayMentServiceClient;
import com.yuanfeng.job.mapper.UpdateOrderStatusMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName UpdateOrderStatusService
 * @Description 更新订单状态服务
 * @Author liulh
 * @Date 2023/5/17 17:01
 * @Version 1.0
 **/
@Service
public class UpdateOrderStatusService {
    private static final Logger logger = LoggerFactory.getLogger(UpdateOrderStatusService.class);

    @Resource
    private UpdateOrderStatusMapper updateOrderStatusMapper;

    @Autowired
    private PayMentServiceClient payMentServiceClient;

    /**
     * 更新退款超过24小时商家未处理订单为商家同意
     */
    public void updateBusinessOvertimeOrder(){
        //查询退款超过24小时商家未处理订单
        List<BusinessOvertimeOrderDTO> pojoList = updateOrderStatusMapper.queryBusinessOvertimeOrder();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String finishTime = dateformat.format(new Date());
        if (pojoList.size() >= 1) {
            for (BusinessOvertimeOrderDTO pojo : pojoList) {
                pojo.setReturnShopHandle(5);
                pojo.setReturnState(5);
                pojo.setReturnShopMessage("超时未处理，自动同意");
                pojo.setReturnShopTime(finishTime);
                pojo.setReturnFinishTime(finishTime);
                pojo.setOrderStatus(6);
                pojo.setOrderRefundStatus(2);
                pojo.setOrderFinishedTime(finishTime);
                String uOrderId = updateOrderStatusMapper.queryPaymentNumber(pojo);
                List<Integer> orderTradeStatus = updateOrderStatusMapper.queryOrderRecordStatus(uOrderId);
                RefundParamDTO refundPara = new RefundParamDTO();
                boolean isRefundSuccess = false;
                String orderId = pojo.getOrderId();
                RefundParamTwoDTO refundParaTwo = updateOrderStatusMapper.queryRefundPara(orderId);
                refundPara.setOrderFrom(refundParaTwo.getOrderFrom());
                refundPara.setOrderId(refundParaTwo.getOrderId());
                refundPara.setOrderTradeStatus(orderTradeStatus);
                refundPara.setPayCode(refundParaTwo.getPayCode());
                refundPara.setReturnCode(refundParaTwo.getReturnCode());
                refundPara.setPaymentOtherNumber(refundParaTwo.getPaymentOtherNumber());
                refundPara.setReturnCash(pojo.getReturnCash());
                logger.info("========------");
                //调用退款接口
                isRefundSuccess = payMentServiceClient.waitShipmentsRefund(refundPara);
                System.out.println(isRefundSuccess);
                logger.info(isRefundSuccess+"");
                logger.info("========");
                //退还红包、优惠券
                if(isRefundSuccess == false){
                    logger.info("====退款false 跳过====");
                    continue;
                }else{
                    logger.info("====更新订单状态====");
                    //更新订单表状态
                    Integer torf = updateOrderStatusMapper.updateBusinessOvertimeOrderBase(pojo);
                    //更新退款表状态
                    updateOrderStatusMapper.updateBusinessOvertimeOrderReturn(pojo);
                }
            }
        }
        logger.info("更新超过24小时商家未处理订单  更新时间" + TimeStampUtil.getNowDate());
    }


    public List<String> queryUnpaidOrdersToBeCancelled() {
        return updateOrderStatusMapper.queryUnpaidOrdersToBeCancelled();
    }

    public List<AddInventoryDTO> queryGoodsDetails(String orderId) {
        return updateOrderStatusMapper.queryGoodsDetails(orderId);
    }

    public void updateSaleSum2(WapGoodsSaleSumUpdateVO wgssu) {
        updateOrderStatusMapper.updateSaleSum2(wgssu);
    }

    public boolean updateByOrderBase(String orderId, Integer orderStatus, Integer orderCancelIdentity, String orderCancelReason, String orderCancelDate) {
        return updateOrderStatusMapper.updateByOrderBase(orderId,orderStatus,orderCancelIdentity,orderCancelReason,orderCancelDate);
    }

    public boolean updateByOrderGoods(String orderId, Integer orderStatus) {
        return updateOrderStatusMapper.updateByOrderGoods(orderId,orderStatus);
    }

    public void updateOrderStatus(Integer status, String orderId) {
        updateOrderStatusMapper.updateOrderStatus(status,orderId);
    }

    public CancelParaEntityDTO queryPaymentNumberAndRedpacketId(String orderId) {
        return updateOrderStatusMapper.queryPaymentNumberAndRedpacketId(orderId);
    }

    public List<WapGoodsSaleSumDTO> queryGoodsXinXi(String orderId) {
        return updateOrderStatusMapper.queryGoodsXinXi(orderId);
    }

    public List<Integer> queryOrderStatus(String paymentNumber) {
        return updateOrderStatusMapper.queryOrderStatus(paymentNumber);
    }
}
